package com.timoon.webcomponent.utility.titlelist.typelist;

import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.timoon.app.resource.image.ImageLocation;
import com.timoon.page.Page1;
import com.timoon.sample.dao.model.InfoSort;

/**
 *<p>通过info_sort_type提取info_sort[list],用于列表显示</p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */

public class PasssageTypeListPanel extends Panel {

	@SpringBean(name="sample.infoSort")
	private PasssageTypeListService service;
	
	private Integer infoType=1;//info_sort_type
	
	private Class<? extends WebPage> targetPage;
	
	private PageParameters pageParameters;
	
	public PasssageTypeListPanel(String id,Integer infoType,PageParameters pageParameters,Class<? extends WebPage> targetPage) {
		super(id);
		// TODO Auto-generated constructor stub
		
		this.pageParameters=pageParameters;
		
		this.infoType=infoType;
		
		this.targetPage=targetPage;
		
		renderPanel();
		
		//renderImg();
	}

	private void renderImg() {
		// TODO Auto-generated method stub
		add(new Image("wk_img", new PackageResourceReference(ImageLocation.class,"other/news.jpg")));
	}

	private void renderPanel() {
		// TODO Auto-generated method stub
		
		Integer activeSortId=-1;
		if(!this.pageParameters.get("actionInfoSortId").isNull()){
			activeSortId=this.pageParameters.get("actionInfoSortId").toInteger();
		}
		final Integer infoSortId=activeSortId;
		
		List<InfoSort> iss=this.service.getInfoSortListByType(infoType);
		
		ListView<InfoSort> listView=new ListView<InfoSort>("wk_list",iss) {
			
			@Override
			protected void populateItem(ListItem<InfoSort> arg0) {
				// TODO Auto-generated method stub
				
				InfoSort is=arg0.getModelObject();
				
				PageParameters parameters=new PageParameters();
				parameters.set("passage_type", is.getValue());
				parameters.set("actionInfoSortId", is.getId());
				
				BookmarkablePageLink link=new BookmarkablePageLink<>("wk_link", targetPage,parameters);
				arg0.add(link);
				
				link.add(new Label("wk_label",is.getName()));
				
				//active
				if(infoSortId>0){
					if(is.getId().compareTo(infoSortId)==0){
						arg0.add(new AttributeModifier("class", "active"));
					}
				}
			}
		};
		add(listView);
	}

}
